package com.testing.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
public class Account {

    private String name;

    private double balance;

    /**线程的调度室没有规律的，当主线程跑完后，子线程就会停止**/
    public synchronized void setAccount(String name, double balance){
        this.name = name;
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.balance = balance;
    }

    public synchronized double getBalance(String name) {
        return balance;
    }

    public static void main(String[] args) {
        Account ac  = new Account();
        new Thread(() -> ac.setAccount("nam",100)).start();
        try {
            TimeUnit.MICROSECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(ac.getBalance("name"));
        try {
            TimeUnit.MICROSECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ac.getBalance("name"));
    }
}
